package com.itheima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.article.dto.ArticleHomeDto;
import com.itheima.article.dto.ArticleInfoDto;
import com.itheima.article.mapper.ApArticleConfigMapper;
import com.itheima.article.mapper.ApArticleContentMapper;
import com.itheima.article.pojo.ApArticle;
import com.itheima.article.mapper.ApArticleMapper;
import com.itheima.article.pojo.ApArticleConfig;
import com.itheima.article.pojo.ApArticleContent;
import com.itheima.article.service.ApArticleContentService;
import com.itheima.article.service.ApArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.article.vo.ArticleInfoVo;
import com.itheima.common.constants.BusinessConstants;
import com.itheima.common.exception.LeadNewsException;
import com.itheima.common.vo.PageResultVo;
import com.itheima.common.vo.ResultVo;
import com.itheima.wemedia.feign.WmNewsFeign;
import com.itheima.wemedia.pojo.WmNews;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @description <p>已发布的文章信息 业务实现</p>
 *
 * @version 1.0
 * @package com.itheima.article.service.impl
 */
@Service
@Slf4j
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {

    @Autowired
    private ApArticleContentMapper apArticleContentMapper;

    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @Autowired
    private WmNewsFeign wmNewsFeign;

    /**
     * 同步文章微服务到文章
     * @param dto
     */
    @Override
    @Transactional
    public void saveArticleInfo(ArticleInfoDto dto) {
        log.info("文章微服务同步文章信息开始");
        //1.保存文章信息
        ApArticle apArticle = dto.getApArticle();
        //旧的文章id
        Long articleId = apArticle.getId();
        if (null != articleId){
            ApArticleConfig apArticleConfig = new ApArticleConfig();
            apArticleConfig.setIsDelete(1);
            apArticleConfig.setId(articleId);
            apArticleConfigMapper.updateById(apArticleConfig);
        }
        save(apArticle);
        log.info("文章信息保存保存成功");
        //获取新文章的id
        Long id = apArticle.getId();
        //2.保存文章内容
        ApArticleContent apArticleContent = dto.getApArticleContent();
        apArticleContent.setArticleId(id);
        apArticleContentMapper.insert(apArticleContent);
        log.info("文章内容保存成功");
        //3.保存文章配置
        ApArticleConfig apArticleConfig = dto.getApArticleConfig();
        apArticleConfig.setArticleId(id);
        apArticleConfig.setId(id);
        apArticleConfigMapper.insert(apArticleConfig);
        log.info("文章配置保存成功");
        //4.更新自媒体的文章状态
        WmNews wmNews = new WmNews();
        wmNews.setId(dto.getWmNewsId());
        wmNews.setArticleId(id);
        //状态已发布
        wmNews.setStatus(BusinessConstants.WmNewsConstants.STATUS_PUBLISHED);
        ResultVo<WmNews> updateResult = wmNewsFeign.update(wmNews);
        if (!updateResult.isSuccess()){
            throw new LeadNewsException("远程调用更新自媒体文章状态失败. 文章数据同步");
        }
        log.info("远程调用自媒体更新文章状态成功");
    }

    /**
     * app端，文章页面分页查询
     * @param dto
     * @param loadNew
     * @return
     */
    @Override
    public PageResultVo<ApArticle> load(ArticleHomeDto dto, int loadNew) {
        //1.参数校验
        dto.checkSize();
        //2.查询
        List<ApArticle> list = getBaseMapper().load(dto,loadNew);
        //3.构建分页结果返回
        return PageResultVo.pageResult(dto.getLoaddir(),dto.getSize(),1000l,list);
    }

    /**
     * 加载文章详情
     * @param paramap
     * @return
     */
    @Override
    public ArticleInfoVo loadArticleInfo(Map<String, String> paramap) {
         //1.获取文章id
        long articleId = Long.parseLong(paramap.get("articleId"));
        //2.查询文章配置信息
        LambdaQueryWrapper<ApArticleConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ApArticleConfig::getArticleId,articleId);
        wrapper.eq(ApArticleConfig::getIsDelete,0);
        wrapper.eq(ApArticleConfig::getIsDown,0);
        ApArticleConfig apArticleConfig = apArticleConfigMapper.selectOne(wrapper);
        if (null == apArticleConfig){
            throw new LeadNewsException("文章已删除或已下架");
        }
        //3.查询文章内容
        LambdaQueryWrapper<ApArticleContent> contentWrapper = new LambdaQueryWrapper<>();
        contentWrapper.eq(ApArticleContent::getArticleId,articleId);
        ApArticleContent apArticleContent = apArticleContentMapper.selectOne(contentWrapper);
        //4.封装vo
        ArticleInfoVo articleInfoVo = new ArticleInfoVo();
        articleInfoVo.setConfig(apArticleConfig);
        articleInfoVo.setContent(apArticleContent);
        return articleInfoVo;
    }
}
